package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.platform.NetConfigDTO;
import com.ayf.payment.game.api.dto.platform.NetQqDTO;
import com.ayf.payment.game.api.dto.platform.NetRateViewDTO;
import com.ayf.payment.game.api.dto.platform.NetRechargeUrlDTO;
import com.ayf.payment.game.api.entity.platform.NetConfig;
import com.ayf.payment.game.api.entity.platform.NetQq;
import com.ayf.payment.game.api.entity.platform.NetRechargeUrl;
import com.ayf.payment.game.api.mapper.platform.NetConfigMapper;
import com.ayf.payment.game.api.service.platform.NetConfigService;
import com.ayf.payment.game.api.service.platform.NetQqService;
import com.ayf.payment.game.api.service.platform.NetRateViewService;
import com.ayf.payment.game.api.service.platform.NetRechargeUrlService;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class NetConfigServiceImpl implements NetConfigService {

    @Resource
    private NetConfigMapper netConfigMapper;

    @Autowired
    private NetRechargeUrlService netRechargeUrlService;
    @Resource
    private NetQqService netQqService;
    @Resource
    private NetRateViewService netRateViewService;

    @Override
    public void update(NetConfigDTO netConfigDTO) {
        NetConfig netConfig=netConfigMapper.selectByPrimaryKey(netConfigDTO.getId());
        netConfig.setNetName(netConfigDTO.getNetName());
        netConfig.setCopyright(netConfigDTO.getCopyright());
        netConfig.setDomainName(netConfigDTO.getDomainName());
        netConfig.setRechargeReturnDomain(netConfigDTO.getRechargeReturnDomain());
        netConfig.setSettleBank(netConfigDTO.getSettleBank());
        netConfig.setTrafficStatisticsCode(netConfigDTO.getTrafficStatisticsCode());
        netConfig.setUrlHeader(netConfigDTO.getUrlHeader());
        netConfig.setPlatContactInfo(netConfigDTO.getPlatContactInfo());
        netConfig.setBoxHideKey(netConfigDTO.getBoxHideKey());
        netConfigMapper.updateByPrimaryKey(netConfig);
        netRechargeUrlService.delNetRechargeUrlAllByIds(netConfigDTO.getNetRechargeUrlIds());
        List<NetRechargeUrl> list = BeanConvertUtils.convert(netConfigDTO.getNetRechargeUrlList(), NetRechargeUrl.class);
        netRechargeUrlService.insertList(list);

        List<NetQq> NetQqDTOList = BeanConvertUtils.convert(netConfigDTO.getNetQqDTOList(), NetQq.class);
        netQqService.insertList(NetQqDTOList);
        netRateViewService.update(netConfigDTO.getNetRateViewDTOList());
    }

    @Override
    public NetConfigDTO getNetConfig() {
        NetConfig netConfigDB=netConfigMapper.getNetConfig();
        NetConfigDTO netConfigDTO=new NetConfigDTO();
        netConfigDTO.setId(netConfigDB.getId());
        netConfigDTO.setCopyright(netConfigDB.getCopyright());
        netConfigDTO.setDomainName(netConfigDB.getDomainName());
        netConfigDTO.setNetName(netConfigDB.getNetName());
        netConfigDTO.setRechargeReturnDomain(netConfigDB.getRechargeReturnDomain());
        netConfigDTO.setSettleBank(netConfigDB.getSettleBank());
        netConfigDTO.setTrafficStatisticsCode(netConfigDB.getTrafficStatisticsCode());
        netConfigDTO.setUrlHeader(netConfigDB.getUrlHeader());
        netConfigDTO.setSettleBank(netConfigDB.getSettleBank());
        netConfigDTO.setBoxHideKey(netConfigDB.getBoxHideKey());
        netConfigDTO.setPlatContactInfo(netConfigDB.getPlatContactInfo());
        List<NetRechargeUrl> list=netRechargeUrlService.selectAll();
        if (list == null) {
            list = new ArrayList<>();
        }
        List<NetRechargeUrlDTO> listDto = BeanConvertUtils.convert(list, NetRechargeUrlDTO.class);
        netConfigDTO.setNetRechargeUrlList(listDto);

        List<NetQq> netQqList = netQqService.list();
        if (netQqList == null) {
            netQqList = new ArrayList<>();
        }
        List<NetQqDTO> netQqListDto = BeanConvertUtils.convert(netQqList, NetQqDTO.class);
        netConfigDTO.setNetQqDTOList(netQqListDto);

        List<NetRateViewDTO> listNetRateViewDTO = netRateViewService.list();
        if (listNetRateViewDTO == null) {
            listNetRateViewDTO = new ArrayList<>();
        }
        netConfigDTO.setNetRateViewDTOList(listNetRateViewDTO);
        return netConfigDTO;
    }

    @Override
    public NetConfigDTO getNetConfigDTO() {
        return BeanConvertUtils.convert(netConfigMapper.getNetConfig(),NetConfigDTO.class);
    }
}
